package com.yswl.yeeduo.ui.activity;

import java.util.Map;
import java.util.TreeMap;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.umeng.analytics.MobclickAgent;
import com.yswl.yeeduo.App;
import com.yswl.yeeduo.R;
import com.yswl.yeeduo.http.KAsyncTask.TaskDelegate;
import com.yswl.yeeduo.tool.KStringTool;
import com.yswl.yeeduo.ui.base.BaseActivity;
import com.yswl.yeeduo.ui.exception.NetworkException;
import com.yswl.yeeduo.ui.task.FoxPwdTask;
import com.yswl.yeeduo.ui.tool.ServiceAuthManager;
import com.yswl.yeeduo.ui.tool.ToastUtil;
import com.yswl.yeeduo.ui.tool.UserManger;

public class FoxPwdActivity extends BaseActivity implements OnClickListener, TaskDelegate {

	EditText mPhone;
	EditText mRePwd;
	EditText mNewPwd;
	Button mConfirm;
	String userName;
	String userPwd;
	String newPwd;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_foxpwd);
		setHeader("修改密码");
		initView();

	}

	private void initView() {
		mPhone = (EditText) findViewById(R.id.phone_et);
		mRePwd = (EditText) findViewById(R.id.old_pwd_edit);
		mNewPwd = (EditText) findViewById(R.id.new_pwd_edit);
		mConfirm = (Button) findViewById(R.id.confirm_btn);
		mConfirm.setOnClickListener(this);

		if (App.getInstance().isLogin) {
			String name = UserManger.getInstance().getUserName(this);
			mPhone.setText(name);
		}
	}

	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		case R.id.confirm_btn:// 提交
			userName = mPhone.getText().toString().trim();
			userPwd = mRePwd.getText().toString().trim();
			newPwd = mNewPwd.getText().toString().trim();

			if (KStringTool.isNotEmpty(newPwd) && KStringTool.isNotEmpty(userPwd)&& KStringTool.isNotEmpty(userName) ) {
				Map<String, String> map = new TreeMap<String, String>();
				map.put("phone", userName);
				map.put("passwd", userPwd);
				map.put("newPasswd", newPwd);
				new FoxPwdTask(this, 10021, this).execute(map);
			}

			break;

		default:
			break;
		}
	}



	@Override
	public void faild_TaskDelegate(int taskId, Object object) {
		if (object instanceof NetworkException) {
			ToastUtil.showShortMsg("网络异常");
			return;
		}
		if (taskId == 10021) {
			ToastUtil.showShortMsg("该手机号码已经注册，请登录");
		}
	}

	@Override
	public void success_TaskDelegate(int taskId, Object object) {
		if (taskId == 10021 && (Boolean) object) {
			ToastUtil.showShortMsg("修改密码成功，请重新登录");
			App.getInstance().isLogin = false;
		    ServiceAuthManager.saveAuthParams(this, "", "", "");
			startActivity(new Intent(this, LoginActivity.class));
			finish();			
		}
	}

	// Um
		public void onResume() {
			super.onResume();
			MobclickAgent.onPageStart(this.getClass().getSimpleName());
			MobclickAgent.onResume(this); // 统计时长
		}

		public void onPause() {
			super.onPause();
			MobclickAgent.onPageEnd(this.getClass().getSimpleName());
			MobclickAgent.onPause(this);
		}
}
